package cn.com.bluemoon.daps.sync.core.sync.log;

import cn.com.bluemoon.daps.common.enums.SyncToEnv;
import cn.com.bluemoon.daps.sync.core.sync.GlobalConfig;
import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.HashSet;
import java.util.Set;

/**
 * 发送给开发同步还是发布同步
 *
 * @author Jarod.Kong
 * @date 2021/6/25 13:31
 */
@Slf4j
public enum SyncEnvEventFactory {
    ;

    public static void publishEvent(GlobalConfig globalConfig, SyncEnvLog envLog, EvenType type) {
        if (envLog instanceof SyncEnvProdLog) {
            SpringUtil.getApplicationContext().publishEvent(new SyncEnvProdLogEvent((SyncEnvProdLog)envLog, type, globalConfig));
        } else {
            SpringUtil.getApplicationContext().publishEvent(new SyncEnvLogEvent(envLog, type));
        }
    }

    public static final Set<SyncToEnv> syncEnvProLog = new HashSet<SyncToEnv>(){{
        add(SyncToEnv.PRE_PROD);
        add(SyncToEnv.PROD);
    }};

    public static SyncEnvLog createLog(GlobalConfig globalConfig,SyncToEnv syncToEnv){
        if (syncToEnv == null) {
            syncToEnv = globalConfig.getSyncToEnv();
        }
        if (syncEnvProLog.contains(syncToEnv)){
            return SyncEnvProdLog.createLog(globalConfig);
        }
        return SyncEnvLog.createLog(globalConfig);
    }
}
